package com.lmiot.xyclick.Util;

import android.os.Environment;
import com.lmiot.xyclick.App.MyApp;
import com.lkx.liandianqinew.R;
import com.youyi.yychosesdk.utils.file.FileUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;

public class RawUtils {
    public static void getAllRawFile() {
        Field[] declaredFields = R.raw.class.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            try {
                String name = declaredFields[i].getName();
                int i2 = declaredFields[i].getInt(R.raw.class);
                if (name.startsWith("a2020")) {
                    saveToFile(i2, new File(Environment.getExternalStorageDirectory() + "/XYClick/data", name.replace("a2020", "2020") + "." + DataUtil.FILE_XYCLICK_DATA).getAbsolutePath());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void saveToFile(int i, String str) {
        try {
            InputStream openRawResource = MyApp.getContext().getResources().openRawResource(i);
            byte[] bArr = new byte[1024];
            FileOutputStream fileOutputStream = new FileOutputStream(str);
            while (true) {
                int read = openRawResource.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                    fileOutputStream.flush();
                } else {
                    openRawResource.close();
                    fileOutputStream.close();
                    return;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
